<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简历上传解析测试</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .container {
            background: white;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        
        .upload-section {
            border: 2px dashed #ddd;
            border-radius: 8px;
            padding: 30px;
            text-align: center;
            margin-bottom: 30px;
            transition: border-color 0.3s;
        }
        
        .upload-section:hover {
            border-color: #007bff;
        }
        
        .file-input {
            margin: 20px 0;
        }
        
        .file-input input[type="file"] {
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 100%;
            max-width: 400px;
        }
        
        .btn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px;
        }
        
        .btn:hover {
            background-color: #0056b3;
        }
        
        .btn:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        
        .result-section {
            margin-top: 30px;
        }
        
        .result-box {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 20px;
            margin: 15px 0;
        }
        
        .error {
            background-color: #f8d7da;
            border-color: #f5c6cb;
            color: #721c24;
        }
        
        .success {
            background-color: #d4edda;
            border-color: #c3e6cb;
            color: #155724;
        }
        
        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin: 20px 0;
        }
        
        .info-card {
            background: #fff;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 15px;
        }
        
        .info-card h3 {
            margin: 0 0 10px 0;
            color: #495057;
            font-size: 16px;
        }
        
        .skills-list {
            display: flex;
            flex-wrap: wrap;
            gap: 5px;
        }
        
        .skill-tag {
            background-color: #007bff;
            color: white;
            padding: 2px 8px;
            border-radius: 3px;
            font-size: 12px;
        }
        
        .raw-content {
            max-height: 300px;
            overflow-y: auto;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 14px;
            line-height: 1.4;
        }
        
        .loading {
            display: none;
            text-align: center;
            color: #007bff;
        }
        
        .api-info {
            background-color: #e9ecef;
            border-radius: 4px;
            padding: 15px;
            margin-bottom: 20px;
        }
        
        .api-info code {
            background-color: #f8f9fa;
            padding: 2px 4px;
            border-radius: 2px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>📄 简历上传解析测试</h1>
        
        <div class="api-info">
            <h3>API信息</h3>
            <p><strong>完整版API:</strong> <code>POST /api/resume/upload/</code></p>
            <p><strong>简化版API:</strong> <code>POST /api/resume/upload/simple/</code></p>
            <p><strong>支持格式:</strong> PDF, Word (.doc/.docx), TXT</p>
            <p><strong>最大文件:</strong> 10MB</p>
        </div>
        
        <div class="upload-section">
            <h2>📁 选择简历文件</h2>
            <div class="file-input">
                <input type="file" id="resumeFile" accept=".pdf,.doc,.docx,.txt">
            </div>
            <button class="btn" onclick="uploadResume(false)">使用完整版API解析</button>
            <button class="btn" onclick="uploadResume(true)">使用简化版API解析</button>
        </div>
        
        <div class="loading" id="loading">
            <p>📊 正在解析文件，请稍候...</p>
        </div>
        
        <div class="result-section" id="resultSection" style="display: none;">
            <h2>📋 解析结果</h2>
            <div id="resultContent"></div>
        </div>
    </div>

    <script>
        function uploadResume(useSimpleAPI) {
            const fileInput = document.getElementById('resumeFile');
            const file = fileInput.files[0];
            
            if (!file) {
                alert('请先选择一个文件');
                return;
            }
            
            // 显示加载状态
            document.getElementById('loading').style.display = 'block';
            document.getElementById('resultSection').style.display = 'none';
            
            // 准备FormData
            const formData = new FormData();
            formData.append('resume_file', file);
            
            // 选择API端点
            const apiUrl = useSimpleAPI ? '/api/resume/upload/simple/' : '/api/resume/upload/';
            
            // 发送请求
            fetch(apiUrl, {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('loading').style.display = 'none';
                displayResult(data, useSimpleAPI);
            })
            .catch(error => {
                document.getElementById('loading').style.display = 'none';
                displayError('网络请求失败: ' + error.message);
            });
        }
        
        function displayResult(data, isSimpleAPI) {
            const resultSection = document.getElementById('resultSection');
            const resultContent = document.getElementById('resultContent');
            
            resultSection.style.display = 'block';
            
            if (data.success) {
                if (isSimpleAPI) {
                    // 简化版API结果
                    resultContent.innerHTML = `
                        <div class="result-box success">
                            <h3>✅ ${data.message}</h3>
                        </div>
                        
                        <div class="info-grid">
                            <div class="info-card">
                                <h3>📄 文件信息</h3>
                                <p><strong>文件名:</strong> ${data.file_info.name}</p>
                                <p><strong>文件大小:</strong> ${formatFileSize(data.file_info.size)}</p>
                                <p><strong>内容长度:</strong> ${data.file_info.content_length} 字符</p>
                            </div>
                        </div>
                        
                        <div class="result-box">
                            <h3>📝 解析内容</h3>
                            <div class="raw-content">${escapeHtml(data.content)}</div>
                        </div>
                    `;
                } else {
                    // 完整版API结果
                    const keyInfo = data.data.key_info;
                    const fileInfo = data.data.file_info;
                    
                    resultContent.innerHTML = `
                        <div class="result-box success">
                            <h3>✅ ${data.message}</h3>
                        </div>
                        
                        <div class="info-grid">
                            <div class="info-card">
                                <h3>📄 文件信息</h3>
                                <p><strong>文件名:</strong> ${fileInfo.original_filename}</p>
                                <p><strong>文件大小:</strong> ${formatFileSize(fileInfo.file_size)}</p>
                                <p><strong>文件类型:</strong> ${fileInfo.file_extension}</p>
                                <p><strong>内容长度:</strong> ${fileInfo.content_length} 字符</p>
                            </div>
                            
                            <div class="info-card">
                                <h3>💼 基本信息</h3>
                                <p><strong>工作经验:</strong> ${keyInfo.experience_years} 年</p>
                                <p><strong>教育背景:</strong> ${keyInfo.education || '未识别'}</p>
                                <p><strong>期望地点:</strong> ${keyInfo.preferred_location || '未识别'}</p>
                                <p><strong>期望薪资:</strong> ${keyInfo.expected_salary || '未识别'}</p>
                            </div>
                            
                            <div class="info-card">
                                <h3>🛠️ 技能统计</h3>
                                <p><strong>编程语言:</strong> ${keyInfo.skills.programming_languages.length} 项</p>
                                <p><strong>框架技术:</strong> ${keyInfo.skills.frameworks.length} 项</p>
                                <p><strong>数据库:</strong> ${keyInfo.skills.databases.length} 项</p>
                                <p><strong>开发工具:</strong> ${keyInfo.skills.tools.length} 项</p>
                                <p><strong>总技能数:</strong> ${keyInfo.skills.all_skills.length} 项</p>
                            </div>
                        </div>
                        
                        ${generateSkillsSection(keyInfo.skills)}
                        
                        <div class="result-box">
                            <h3>📝 原始内容</h3>
                            <div class="raw-content">${escapeHtml(data.data.raw_content)}</div>
                        </div>
                    `;
                }
            } else {
                resultContent.innerHTML = `
                    <div class="result-box error">
                        <h3>❌ ${data.message}</h3>
                        ${data.error ? `<p><strong>错误代码:</strong> ${data.error}</p>` : ''}
                    </div>
                `;
            }
        }
        
        function generateSkillsSection(skills) {
            let skillsHtml = '<div class="result-box"><h3>🛠️ 技能详情</h3>';
            
            const skillCategories = [
                { key: 'programming_languages', name: '编程语言' },
                { key: 'frameworks', name: '框架技术' },
                { key: 'databases', name: '数据库' },
                { key: 'tools', name: '开发工具' },
                { key: 'big_data', name: '大数据' },
                { key: 'ai_ml', name: 'AI/机器学习' }
            ];
            
            skillCategories.forEach(category => {
                const skillList = skills[category.key];
                if (skillList && skillList.length > 0) {
                    skillsHtml += `
                        <div style="margin: 15px 0;">
                            <h4>${category.name} (${skillList.length})</h4>
                            <div class="skills-list">
                                ${skillList.map(skill => `<span class="skill-tag">${skill}</span>`).join('')}
                            </div>
                        </div>
                    `;
                }
            });
            
            skillsHtml += '</div>';
            return skillsHtml;
        }
        
        function displayError(message) {
            const resultSection = document.getElementById('resultSection');
            const resultContent = document.getElementById('resultContent');
            
            resultSection.style.display = 'block';
            resultContent.innerHTML = `
                <div class="result-box error">
                    <h3>❌ 处理失败</h3>
                    <p>${message}</p>
                </div>
            `;
        }
        
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
        
        function escapeHtml(text) {
            const div = document.createElement('div');
            div.textContent = text;
            return div.innerHTML;
        }
        
        // 拖拽上传功能
        const uploadSection = document.querySelector('.upload-section');
        
        uploadSection.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadSection.style.borderColor = '#007bff';
            uploadSection.style.backgroundColor = '#f8f9fa';
        });
        
        uploadSection.addEventListener('dragleave', (e) => {
            e.preventDefault();
            uploadSection.style.borderColor = '#ddd';
            uploadSection.style.backgroundColor = 'transparent';
        });
        
        uploadSection.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadSection.style.borderColor = '#ddd';
            uploadSection.style.backgroundColor = 'transparent';
            
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                document.getElementById('resumeFile').files = files;
            }
        });
    </script>
</body>
</html>
